package dbcontrol

import (
	"MqttCloudProtocolServer/publiclib/datamodel/dborm"

	uuid "github.com/satori/go.uuid"
)

//ManageServerControl struct
type ManageServerControl struct {
	base             dbOperabase
	Data             *dborm.Manageserver
	ParentMqttServer *MqttserverControl
	ChildrenMqttconf []*MqttconfControl
}

//CreateTcpconfControl 初始化几种指针函数
func CreateTcpconfControl() *ManageServerControl {
	content := new(ManageServerControl)
	content.base.GetData = func() interface{} {
		return content.Data
	}
	content.base.SetData = func(data interface{}) {
		if data != nil {
			content.Data = data.(*dborm.Manageserver)
		}
	}
	content.base.GetKey = func() interface{} {
		if content.Data == nil {
			return nil
		}
		return content.Data.UUID
	}
	content.base.CreateNew = func() interface{} {
		res := new(dborm.Manageserver)
		uuid := uuid.NewV4()
		res.UUID = uuid.String()
		return res
	}
	content.base.CreateNil = func() interface{} {
		return new(dborm.Manageserver)
	}
	return content
}

//GetAll 获取全部用户-服务器映射信息
func (_this *ManageServerControl) GetAll() ([]*dborm.Manageserver, error) {
	res := make([]*dborm.Manageserver, 0)
	_, err := _this.base.GetAllByPage(&res, 0, 0, 0)
	return res, err
}

//GetAllByPage 获取全部用户-服务器映射信息.totPage<=0时，计算分页页数,返回值第二个参数为计算或传入的页数
func (_this *ManageServerControl) GetAllByPage(totPage, currentPage, countOfPage int) ([]*dborm.Manageserver, int, error) {
	res := make([]*dborm.Manageserver, 0)
	calTotpage, err := _this.base.GetAllByPage(&res, totPage, currentPage, countOfPage)
	return res, calTotpage, err
}

//CreateNew 创建一条记录
func (_this *ManageServerControl) CreateNew() {
	_this.Data = _this.base.CreateNew().(*dborm.Manageserver)
}

//FindByID 根据主键获取一条记录
func (_this *ManageServerControl) FindByID(uuid string) error {
	return _this.base.FindByID(uuid)
}

//Save 更新当前条目
func (_this *ManageServerControl) Save() error {
	return _this.base.Save()
}

//Delete 删除当前条目
func (_this *ManageServerControl) Delete() error {
	return _this.base.Delete()
}

//GetByHostIP fun
func (_this *ManageServerControl) GetByHostIP(ip string) ([]*dborm.Manageserver, error) {
	res := make([]*dborm.Manageserver, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "host_ip = ?", ip)
	return res, err
}

//GetByHostIPAndPort fun
func (_this *ManageServerControl) GetByHostIPAndPort(ip string, port int) ([]*dborm.Manageserver, error) {
	res := make([]*dborm.Manageserver, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "host_ip = ? and host_port = ?", ip, port)
	return res, err
}

//GetByMqttServerID fun
func (_this *ManageServerControl) GetByMqttServerID(mqttID int) ([]*dborm.Manageserver, error) {
	res := make([]*dborm.Manageserver, 0)
	err := _this.base.FindByWhere(&res, 0, 0, "mqtt_server_id = ?", mqttID)
	return res, err
}

//GetParentMqttServer fun
func (_this *ManageServerControl) GetParentMqttServer() error {
	mqttServer := CreateMqttserverControl() // new(MqttserverControl)
	if err := mqttServer.FindByID(_this.Data.MqttServerID); err != nil {
		return err
	}
	_this.ParentMqttServer = mqttServer
	return nil
}

//GetChildrenMqttconf 获取网关MQTT配置
func (_this *ManageServerControl) GetChildrenMqttconf() error {
	mqttconf := CreateMqttconfControl() // new(MqttconfControl)
	res, err := mqttconf.GetByTCPID(_this.Data.UUID)
	if err != nil || res == nil {
		return err
	}
	_this.ChildrenMqttconf = make([]*MqttconfControl, len(res))
	for i, item := range res {
		_this.ChildrenMqttconf[i] = CreateMqttconfControl() //new(MqttconfControl)
		_this.ChildrenMqttconf[i].Data = item
		_this.ChildrenMqttconf[i].ParentTCPConf = _this
	}
	return nil
}

//GetModeName fun
func (_this *ManageServerControl) GetModeName() (string, error) {
	tcpMode := CreateModeenumControl() // new(ModeenumControl)
	if err := tcpMode.FindByID(_this.Data.Mode); err != nil {
		return "", err
	}
	return tcpMode.Data.Name, nil
}

//GetProtocolName fun
func (_this *ManageServerControl) GetProtocolName() (string, error) {
	protocolServer := CreateProtocolenumControl() // new(ProtocolenumControl)
	if err := protocolServer.FindByID(_this.Data.Protocol); err != nil {
		return "", err
	}
	return protocolServer.Data.Name, nil
}
